﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace DataAccessor
{
    /// <summary>
    /// 映射处理类
    /// </summary>
    public abstract partial class IDataAccessor
    {
        /// <summary>
        /// 把IDataReader转变成实例
        /// </summary>
        /// <param name="reader">IDataReader的实例</param>
        /// <param name="statementName">SQL配置节名称</param>
        /// <returns>指定的对象实例。如果没有指定或指定的类型不存在则返回null。</returns>
        public object ToInstance(IDataReader reader, string statementName)
        {
            var statement = StatementArray.Single(s => s.Name.Equals(statementName, StringComparison.CurrentCultureIgnoreCase));
            var mappingAlias = statement.MappingType;
            if (string.IsNullOrEmpty(mappingAlias))
            {
                return null;
            }

            var mappingType = MappingCache.Get(mappingAlias);
            if (mappingType == null)
            {
                return null;
            }

            var mappingTypeName = mappingType.Type;
            var t = Type.GetType(mappingTypeName);
            if (t == null)
            {
                return null;
            }

            var mappingInstance = Activator.CreateInstance(t, null);
            var properties = t.GetProperties();
            foreach (var p in mappingType.Property)
            {
                var property = properties.SingleOrDefault(pp => pp.Name.Equals(p.Field, StringComparison.CurrentCultureIgnoreCase));
                if (property == null)
                {
                    continue;
                }

                property.SetValue(mappingInstance, reader[p.Field], null);
            }

            return mappingInstance;
        }

        /// <summary>
        /// 把DataRow转变成实例
        /// </summary>
        /// <param name="row">DataRow的实例</param>
        /// <param name="statementName">SQL配置节名称</param>
        /// <returns>指定的对象实例。如果没有指定或指定的类型不存在则返回null。</returns>
        public object ToInstance(DataRow row, string statementName)
        {
            var statement = StatementArray.Single(s => s.Name.Equals(statementName, StringComparison.CurrentCultureIgnoreCase));
            var mappingAlias = statement.MappingType;
            if (string.IsNullOrEmpty(mappingAlias))
            {
                return null;
            }

            var mappingType = MappingCache.Get(mappingAlias);
            if (mappingType == null)
            {
                return null;
            }

            var mappingTypeName = mappingType.Type;
            var t = Type.GetType(mappingTypeName);
            if (t == null)
            {
                return null;
            }

            var mappingInstance = Activator.CreateInstance(t, null);
            var properties = t.GetProperties();
            foreach (var p in mappingType.Property)
            {
                var property = properties.SingleOrDefault(pp => pp.Name.Equals(p.Field, StringComparison.CurrentCultureIgnoreCase));
                if (property == null)
                {
                    continue;
                }

                property.SetValue(mappingInstance, row[p.Field], null);
            }

            return mappingInstance;
        }
    }
}
